//
//  AddressBookViewController.m
//  chongwu
//
//  Created by 隼目 on 2021/9/16.
//

#import "AddressBookViewController.h"
#import "AddressBookTableViewCell.h"
#import "AddressViewController.h"
@interface AddressBookViewController ()<UITableViewDelegate,UITableViewDataSource,AddressBookDelegate>
@property(nonatomic,strong)BaseTableView *tableView;
@property(nonatomic,strong)NSMutableArray *tableArr;
@property(nonatomic,assign)NSInteger pageNum;
@end

@implementation AddressBookViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"地址簿";
    
    self.pageNum=1;
    self.tableArr=[NSMutableArray arrayWithCapacity:0];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"xinzeng"] style:UIBarButtonItemStylePlain target:self action:@selector(pushTAddress)];
    
    self.tableView=[BaseTableView new];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(0);
    }];
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.backgroundColor=rgba(245, 246, 246, 1);
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
    self.tableView.mj_header=[MJRefreshHeader headerWithRefreshingBlock:^{
        self.pageNum=1;
        [self.tableArr removeAllObjects];
        [self getDate];
    }];
    [self getDate];
}
-(void)pushTAddress{
    AddressViewController *vc=[[AddressViewController alloc] init];
    vc.typeIndex=self.typeIndex;
    vc.isHidenDog=YES;
    vc.addressBackBlok = ^{
        [self.tableArr removeAllObjects];
        self.pageNum=1;
        [self getDate];
    };
    [ZQTools pushRootViewController:self andNextController:vc];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tableArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    AddressBookTableViewCell *cell=[[AddressBookTableViewCell alloc] init];
    cell.cellDic=self.tableArr[indexPath.row];
    cell.delegate=self;
    [cell creatCellView];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 306*_mainHeight;

}

-(void)getDate{
    NSMutableDictionary *dic=[NSMutableDictionary dictionaryWithCapacity:0];
    [dic setValue:@"20" forKey:@"pageSize"];
    [dic setValue:[NSString stringWithFormat:@"%ld",self.pageNum] forKey:@"pageNum"];
    [self getWithUrl:@"/member/address" param:dic tableView:self.tableView success:^(id  _Nullable dict) {
        [self.tableArr addObjectsFromArray:dict[@"content"][@"records"]];
        [self.tableView reloadData];
        NSLog(@"%@",dict[@"content"][@"records"]);
    } failure:^(NSError * _Nullable error) {
        
    }];
}
-(void)selectAgreement:(NSDictionary *)cellDic{
   
    AddressModel *model=[AddressModel mj_objectWithKeyValues:cellDic];
    [self PutWithUrl:[NSString stringWithFormat:@"/member/address/%@",model.addressId] param:nil tableView:self.tableView success:^(id  _Nullable dict) {
        NSMutableArray *typeArr=[NSMutableArray arrayWithCapacity:0];
        for (NSDictionary *typeDic in self.tableArr) {
            NSMutableDictionary *temporaryDic=[NSMutableDictionary dictionaryWithDictionary:typeDic];
            
            [temporaryDic setValue:@"0" forKey:@"defaultAddress"];
            if (!model.defaultAddress) {
                NSString *addressId=typeDic[@"addressId"];
                if ([addressId isEqualToString:model.addressId]) {
                    [temporaryDic setValue:@"1" forKey:@"defaultAddress"];
                }
            }
            [typeArr addObject:temporaryDic];
        }
        [self.tableArr removeAllObjects];
        [self.tableArr addObjectsFromArray:typeArr];
        [self.tableView reloadData];
    } failure:^(NSError * _Nullable error) {
        
    }];
}
-(void)selectDelete:(NSDictionary *)cellDic{
    
    AddressModel *model=[AddressModel mj_objectWithKeyValues:cellDic];
    [self DeleteWithUrl:[NSString stringWithFormat:@"/member/address/%@",model.addressId] param:nil tableView:self.tableView success:^(id  _Nullable dict) {
        NSMutableArray *typeArr=[NSMutableArray arrayWithArray:self.tableArr];
    
        for (NSDictionary *typeDic in self.tableArr) {
            NSString *addressId=typeDic[@"addressId"];
            if ([addressId isEqualToString:model.addressId]) {
                [typeArr removeObject:typeDic];
            }
        }
        [self.tableArr removeAllObjects];
        [self.tableArr addObjectsFromArray:typeArr];
        [self.tableView reloadData];
    } failure:^(NSError * _Nullable error) {
        
    }];
}

-(void)pushToController:(NSDictionary *)cellDic{
    AddressViewController *vc=[[AddressViewController alloc] init];
    vc.isHidenDog=YES;
    vc.typeIndex=self.typeIndex;
    vc.addressDic=cellDic;
    vc.addressBackBlok = ^{
        [self.tableArr removeAllObjects];
        self.pageNum=1;
        [self getDate];
    };
    [ZQTools pushRootViewController:self andNextController:vc];
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
